<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar title="登录" class="navbar" />
    <!-- 头部导航 -->
    <!-- 手机号/验证码 -->
    <van-form @submit="onSubmit" class="from" ref="from">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="mobileRules"
      >
        <template #label>
          <span class="toutiao toutiao-shouji"></span>
        </template>
      </van-field>
      <van-field
        v-model="code"
        name="code"
        placeholder="请输入验证码"
        :rules="codeRules"
      >
        <template #label>
          <span class="toutiao toutiao-yanzhengma"></span>
        </template>
        <template #button>
          <van-button
            type="default"
            native-type="button"
            v-if="isShowCodeBtn"
            @click="getCode"
            class="code-btn"
            size="small"
            round
            >获取验证码</van-button
          >
          <van-count-down
            v-else
            :time="3 * 1000"
            fromat="ss秒"
            @finish="isShowCodeBtn = true"
          />
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <!-- 手机号/验证码 -->
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import { loginAPI, getCodeAPI } from '@/api'
export default {
  data() {
    const mobileRules = [
      {
        required: true,
        message: '请输入手机号',
        trigger: 'onChange'
      },
      {
        pattern:
          /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
        message: '手机号格式不正确',
        trigger: 'onChange'
      }
    ]
    const codeRules = [
      {
        required: true,
        message: '请输入验证码'
      },
      {
        pattern: /^[0-9]{6}$/,
        message: '请输入正确的验证码'
      }
    ]

    return {
      mobile: '',
      code: '',
      mobileRules,
      codeRules,
      isShowCodeBtn: true
    }
  },
  methods: {
    ...mapMutations(['SET_TOKEN']),
    async onSubmit() {
      const { code, mobile } = this

      this.$toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      try {
        const { data } = await loginAPI(mobile, code)
        // console.log(data

        this.SET_TOKEN(data.data)
        this.$router.push('/profile')
        this.$toast.success('登录成功')
      } catch (error) {
        //  axios 封装对象的错误和 后端返回的状态码
        if (error.response?.status === 400) {
          this.$toast.fail(error.response.message)
        } else {
          //  js导致的错误 或507
          throw error
        }
      }
    },
    async getCode() {
      // 确保用户输入有效手机号
      // 1.给from 表单绑定ref
      // 注意点：传入的是name属性
      await this.$refs.from.validate('mobile')
      //  2.显示倒计时
      this.isShowCodeBtn = false
      //  3.提示loding
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      // 4.发送验证码
      // 此时一定验证成功了手机号
      try {
        await getCodeAPI(this.mobile)
        this.$toast.success('发送验证码成功!')
      } catch (error) {
        if (error.response?.status === 429) {
          // 频繁发送了验证码了
          this.$toast.fail(error.response.data.message)
        } else {
          throw error
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.navbar {
  background-color: #3296fa;
}
:deep(.van-nav-bar__title) {
  color: #fff;
}
:deep(.from) {
  .toutiao {
    font-size: 40px;
  }
  .van-field__label {
    flex: 1;
  }
  .van-field__value {
    flex: 20;
  }
  .code-btn {
    background-color: #eeeeee;
    color: #9a8b7e;
    height: 0.64rem;
  }
}
</style>
